package com.copote.vmp.basedata.model;

import lombok.Data;

import java.io.Serializable;

@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final int COMMON_ERROR_CODE = 500;

    /**
     * 成功标志
     */
    private boolean success = true;

    /**
     * 返回处理消息
     */
    private String message = "";

    /**
     * 返回代码
     */
    private Integer code = 200;

    /**
     * 返回数据对象 data
     */
    private T result;

    /**
     * 时间戳
     */
    private long timestamp = System.currentTimeMillis();

    public Result() {
    }

    public Result(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> ok() {
        return ok(null, null);
    }

    public static <T> Result<T> ok(T data) {
        return ok(data, null);
    }

    public static <T> Result<T> ok(String msg) {
        return ok(null, msg);
    }

    public static <T> Result<T> ok(T data, String msg) {
        Result<T> r = new Result<>();
        r.setResult(data);
        r.setMessage(msg);
        return r;
    }

    public static <T> Result<T> error() {
        return error(COMMON_ERROR_CODE, null, null);
    }

    public static <T> Result<T> error(T data) {
        return error(COMMON_ERROR_CODE, data, null);
    }

    public static <T> Result<T> error(String msg) {
        return error(COMMON_ERROR_CODE, null, msg);
    }

    public static <T> Result<T> error(int code, String msg) {
        return error(code, null, msg);
    }

    public static <T> Result<T> error(T data, String msg) {
        return error(COMMON_ERROR_CODE, data, msg);
    }

    public static <T> Result<T> error(int code, T data, String msg) {
        Result<T> r = new Result<>();
        r.setSuccess(false);
        r.setCode(code);
        r.setResult(data);
        r.setMessage(msg);
        return r;
    }
}
